import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { createTestRunner, createTestTree, defaultAppComponentContent, defaultFiles } from '../utils/testing';

describe('ng-add', () => {
  let appTree: UnitTestTree;
  let testRunner: SchematicTestRunner;

  beforeEach(async () => {
    testRunner = createTestRunner();
    appTree = await createTestTree(testRunner);
  });

  it('判断是否初始化成功', async () => {
    expect(appTree.files).toEqual(defaultFiles);
    expect(appTree.files).toContain('/projects/bar/src/app/app.component.ts');
    expect(appTree.files).toContain('/projects/bar/src/app/app.component.spec.ts');
    expect(appTree.readContent('/projects/bar/src/app/app.component.ts')).toContain(defaultAppComponentContent);
  });


  it('测试ng-add是否执行正确', async () => {
    let tree = await testRunner.runSchematicAsync('ng-add', {}, appTree).toPromise();

    //对比生成的文件名正不正确
    let files = tree.files;
    expect(files).toContain('/projects/bar/app/main.ts');
    let newFiles = [...defaultFiles,
      '/projects/bar/electron-builder.json',
      '/projects/bar/tsconfig.electron.json',
      '/projects/bar/app/main.ts',
      '/projects/bar/app/package-lock.json',
      '/projects/bar/app/package.json',
    ];
    let strings1 = newFiles.filter(s => files.indexOf(s) < 0);
    expect(strings1.length).toBe(0);
    let strings2 = files.filter(s => newFiles.indexOf(s) < 0);
    expect(strings2.length).toBe(0);

    //对比package.json中的scripts正不正确
    let packageJsonContent = tree.readContent('/package.json');
    expect(packageJsonContent).toContain('\n');
    let packageJson = JSON.parse(packageJsonContent);
    expect(packageJson.scripts['postinstall']).toBeDefined();
    expect(packageJson.scripts['electron']).toBeDefined();
    expect(packageJson.scripts['electron:serve']).toBeDefined();
    expect(packageJson.scripts['electron:start']).toBeDefined();
    expect(packageJson.scripts['electron:serve-tsc']).toBeDefined();
    expect(packageJson.scripts['electron:build']).toBeDefined();
    expect(packageJson.scripts['electron:build-mac']).toBeDefined();
    expect(packageJson.scripts['electron:build-windows']).toBeDefined();
    expect(packageJson.scripts['electron:build-linux']).toBeDefined();

    //对比package.json中的devDependencies正不正确
    expect(packageJson.devDependencies['electron']).toBeDefined();
    expect(packageJson.devDependencies['electron-builder']).toBeDefined();
    expect(packageJson.devDependencies['electron-debug']).toBeDefined();
    expect(packageJson.devDependencies['electron-reloader']).toBeDefined();
    expect(packageJson.devDependencies['npm-run-all']).toBeDefined();
    expect(packageJson.devDependencies['wait-on']).toBeDefined();

    //对比package.json中的main正不正确
    expect(packageJson.main).toBe('app/main.js');
  });
});
